<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item label="发布日期">
          <el-date-picker
            v-model="listQuery.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发布日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search" size="small"
            >查询</el-button
          >
          <el-button @click="reset" size="small">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="downloadLoading"
            type="success"
            icon="el-icon-plus"
            @click="handleEdit"
            size="small"
          >
            新增
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      key="id"
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
    >
      <el-table-column label="序号" type="index" width="50"> </el-table-column>
      <el-table-column label="标题" prop="title" show-overflow-tooltip>
      </el-table-column>

      <el-table-column label="发布日期" prop="create_time"> </el-table-column>

      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template slot-scope="{ row, $index }">
          <el-button type="text" size="mini" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button
            style="margin-left: 10px"
            slot="reference"
            v-if="row.user_name != 'admin'"
            class="red"
            type="text"
            size="mini"
            @click="remove(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchList, remove } from "@/api/helpers";
import Pagination from "@/components/Pagination";

export default {
  name: "help",
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        date: undefined,
      },
      info: {},
      downloadLoading: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      const res = await fetchList(this.listQuery);
      if (res.data) {
        this.list = res.data.list;
        this.total = res.data.pageParam.total;
      }
      this.listLoading = false;
    },
    // 查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    reset() {
      this.listQuery = {
        page: 1,
        pageSize: 20,
        date: undefined,
      };
      this.getList();
    },
    // 新增 编辑
    handleEdit(row) {
      this.$router.push({
        path: "/operation/help/info",
        query: {
          id: row.id ? row.id : "",
        },
      });
    },
    async remove(row) {
      this.$confirm("确定删除吗？", "提示").then(async () => {
        const res = await remove({ id: row.id });
        if (res) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.getList();
        }
      });
    },
  },
};
</script>
